// pages/order/order.js
const app = getApp()
const tools = require('../../utils/util')
Page({
  data:{
    orderList:[],
    sum: 0,
    HZL_my_cat: [],
    allNum: 0,
    orderCode: "",
  },
  /**
 * 生命周期函数--监听页面加载
 */
onLoad: function () {
  this.getOrderList()
  console.log('HZL_my_cat', wx.getStorageSync('HZL_my_cat'))
  var HZL_my_cat = wx.getStorageSync('HZL_my_cat')
  var sum = 0;
  var allNum = 0;
  for (let i = 0; i < HZL_my_cat.length; i++) {
    sum = sum + HZL_my_cat[i].num * HZL_my_cat[i].price
    allNum = allNum + HZL_my_cat[i].num
  }
  console.log(sum)
  console.log(allNum)
  console.log('user_table--------', wx.getStorageSync('user_table'))
  // 存放订单号
  let orderCode = '';
  // 6位随机数(加在时间戳后面)
  for (var j = 0; j < 6; j++)
  {
    orderCode += Math.floor(Math.random() * 10);
  }
  // 时间戳(用来生成订单号)
  orderCode = 'D' + new Date().getTime() + orderCode;
  // 打印
  console.log("orderCode",orderCode)
  this.setData({
    user_table: wx.getStorageSync('user_table'),
    HZL_my_cat: wx.getStorageSync('HZL_my_cat'),
    sum: sum,
    allNum: allNum,
    orderCode: orderCode
  })
},

onShow:function(){
  this.getOrderList()
 },
 
/**
 * 获取订单列表
 */
 getOrderList(){
  let that = this
  wx.request({
    url: 'https://order.xiaxueruo.com/api/order/list?user_id='+wx.getStorageSync('user_id'),
    header: {
      'content-type': 'application/json'
    },
    method: 'GET',//get或者post
    success: function (res) {
      console.log(res)//接口返回值
      console.log('1111111',res.data.data.orderList)
      // console.log('22222222',res.res.data.data.orderList[0].detail)
      that.setData({
        orderList: res.data.data.orderList
      })
    }
  })
  
 },
})